package com.ourteam.product.model.dao.impl;

import com.ourteam.product.model.dao.IProductModelActionDAO;
import com.ourteam.product.model.dao.ProductModelAction;
import com.ourteam.product.model.dao.ProductModelActionDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 IRKT 2017
 * Auto Generate Date : 2017-11-17 11:01:21
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductModelActionDAOJdbcImpl extends AbstractSpringDAO
    implements IProductModelActionDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ObjectId, PropertyId, Action, Operation, PropertyValue, Status,
            Remarks, PropertyName, PropertyDataType
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ObjectId,
            _DataObjectProperty.PropertyId, _DataObjectProperty.Action,
            _DataObjectProperty.Operation, _DataObjectProperty.PropertyValue,
            _DataObjectProperty.Status, _DataObjectProperty.Remarks,
            _DataObjectProperty.PropertyName,
            _DataObjectProperty.PropertyDataType
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductModelAction, ProductModelActionDAOQueryBean> daoHelper;

    public AbstractProductModelActionDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductModelAction, ProductModelActionDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.model.dao.IProductModelPropertyDAO.EntityName)
                                .append(" MODEL_PROPERTY ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.model.dao.IProductModelPropertyDAO.Id,
                    com.ourteam.product.model.dao.IProductModelPropertyDAO.EntityName +
                    ".", "MODEL_PROPERTY."));
            relatedJoinSQLBuffer.append(" = ").append(PropertyId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductModelAction
         * @throws Exception
         */
    public boolean checkIsUnique(ProductModelAction aProductModelAction)
        throws Exception {
        if (aProductModelAction == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductModelAction.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductModelAction
     * @throws Exception
     */
    public void insert(ProductModelAction aProductModelAction)
        throws Exception {
        super.doInsert(aProductModelAction);
    }

    /**
         * Batch Insert Datas
         * @param aProductModelAction
         * @throws Exception
         */
    public void batchInsert(ProductModelAction[] aProductModelActions)
        throws Exception {
        super.doBatchInsert(aProductModelActions);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductModelActionDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductModelAction aProductModelAction)
        throws Exception {
        delete(aProductModelAction.getId());
    }

    /**
     * Update Data Selective
     * @param aProductModelAction
     * @throws Exception
     */
    public void update(ProductModelAction aProductModelAction)
        throws Exception {
        if (aProductModelAction.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductModelAction);

        ProductModelAction tempObj = findById(aProductModelAction.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductModelAction, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductModelActions[]
     * @throws Exception
     */
    public void batchUpdate(ProductModelAction[] aProductModelActions)
        throws Exception {
        super.doBatchUpdate(aProductModelActions);
    }

    /**
         * Update Data By Condictions
         * @param aProductModelAction
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductModelAction aProductModelAction,
        ProductModelActionDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductModelAction, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductModelActionDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductModelAction aProductModelAction)
        throws Exception {
        if (aProductModelAction.getAction() == null) {
            throw new Exception(
                "The ProductModelAction.action value is not allow NULL");
        }

        if (aProductModelAction.getOperation() == null) {
            throw new Exception(
                "The ProductModelAction.operation value is not allow NULL");
        }

        if (aProductModelAction.getPropertyValue() == null) {
            throw new Exception(
                "The ProductModelAction.propertyValue value is not allow NULL");
        }

        if (aProductModelAction.getStatus() == null) {
            throw new Exception(
                "The ProductModelAction.status value is not allow NULL");
        }

        if (aProductModelAction.getRemarks() == null) {
            throw new Exception(
                "The ProductModelAction.remarks value is not allow NULL");
        }

        if (aProductModelAction.getPropertyName() == null) {
            throw new Exception(
                "The ProductModelAction.propertyName value is not allow NULL");
        }

        if (aProductModelAction.getPropertyDataType() == null) {
            throw new Exception(
                "The ProductModelAction.propertyDataType value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductModelAction findById(final long id) throws Exception {
        ProductModelActionDAOQueryBean queryBean = new ProductModelActionDAOQueryBean();

        ProductModelActionDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductModelAction[] ProductModelActions = query(queryBean);

        if (ProductModelActions.length == 0) {
            return null;
        } else {
            return ProductModelActions[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductModelAction[]
         * @throws Exception
         */
    public ProductModelAction[] query(ProductModelActionDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductModelActionRowMapper());

        return (ProductModelAction[]) result.toArray(new ProductModelAction[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductModelActionDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductModelActionDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductModelAction[] aProductModelActions,
        ProductModelActionDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductModelActions, queryBean);
    }

    public void synchronize(ProductModelAction[] aProductModelActions,
        ProductModelActionDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductModelAction> callBack)
        throws Exception {
        daoHelper.synchronize(aProductModelActions, queryBean, callBack);
    }

    public static class JdbcProductModelActionRowMapper implements RowMapper<ProductModelAction> {
        public ProductModelAction mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductModelAction object = new ProductModelAction();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object objectId = null;

                objectId = resultSet.getObject(columnIndex++);

                object.setObjectIdNull(resultSet.getBoolean(columnIndex++));

                if (objectId != null) {
                    setBeanValue(object, _DataObjectProperty.ObjectId, objectId);
                }

                Object propertyId = null;

                propertyId = resultSet.getObject(columnIndex++);

                object.setPropertyIdNull(resultSet.getBoolean(columnIndex++));

                if (propertyId != null) {
                    setBeanValue(object, _DataObjectProperty.PropertyId,
                        propertyId);
                }

                Object action = null;

                action = resultSet.getObject(columnIndex++);

                object.setActionNull(resultSet.getBoolean(columnIndex++));

                if (action != null) {
                    setBeanValue(object, _DataObjectProperty.Action, action);
                }

                Object operation = null;

                operation = resultSet.getObject(columnIndex++);

                object.setOperationNull(resultSet.getBoolean(columnIndex++));

                if (operation != null) {
                    setBeanValue(object, _DataObjectProperty.Operation,
                        operation);
                }

                Object propertyValue = null;

                propertyValue = resultSet.getObject(columnIndex++);

                object.setPropertyValueNull(resultSet.getBoolean(columnIndex++));

                if (propertyValue != null) {
                    setBeanValue(object, _DataObjectProperty.PropertyValue,
                        propertyValue);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object propertyName = null;

                propertyName = resultSet.getObject(columnIndex++);

                object.setPropertyNameNull(resultSet.getBoolean(columnIndex++));

                if (propertyName != null) {
                    setBeanValue(object, _DataObjectProperty.PropertyName,
                        propertyName);
                }

                Object propertyDataType = null;

                propertyDataType = resultSet.getObject(columnIndex++);

                object.setPropertyDataTypeNull(resultSet.getBoolean(
                        columnIndex++));

                if (propertyDataType != null) {
                    setBeanValue(object, _DataObjectProperty.PropertyDataType,
                        propertyDataType);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
